package com.caiden_micheal.menu.utils;

import com.caiden_micheal.menu.service.MenuService;
import com.caiden_micheal.permission.domain.Permission;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class MenuUtil {
    public static Boolean checkMenuPermission(String permissionAlias) {
        if (permissionAlias.length() < 9) {
            return false;
        } else {
            return ":menuView".equals(permissionAlias.substring(permissionAlias.length() - 9));
        }
    }

    public static void permissionListMerging(List<Permission> permissionList, MenuService menuService) {
        List<Permission> parentPermissionList = new ArrayList<>();
        List<Long> parentPermissionIdList = new ArrayList<>();
        for (Permission permission : permissionList) {
            if (MenuUtil.checkMenuPermission(permission.getAuthorityAlias())) {
                List<Permission> tempParentPermission = menuService.findAllParentMenuPermissionByPermission(permission);
                if (tempParentPermission != null && tempParentPermission.size() != 0) {
                    for (Permission permissionInner : tempParentPermission) {
                        if (!parentPermissionIdList.contains(permissionInner.getId())) {
                            parentPermissionList.add(permissionInner);
                            parentPermissionIdList.add(permissionInner.getId());
                        }
                    }
                }
            }
        }
        permissionList.addAll(parentPermissionList);
    }

    public static List<Permission> permissionRemoveListMerging(Permission permission, MenuService menuService) {
        List<Permission> permissionList = null;
        if (MenuUtil.checkMenuPermission(permission.getAuthorityAlias())) {
            permissionList = menuService.findAllChildMenuPermissionByPermission(permission);
        }
        if (permissionList != null) {
            permissionList.add(permission);
        } else {
            permissionList = new ArrayList<>();
            permissionList.add(permission);
        }
        HashSet<Permission> set = new HashSet<>(permissionList);
        return new ArrayList<>(set);
    }

    public static List<Permission> excludeExistPermission(List<Long> existsPermissionIdList, List<Permission> permissionList) {
        if (existsPermissionIdList == null || existsPermissionIdList.size() == 0) {
            return permissionList;
        }
        List<Permission> tempPermissionList = new ArrayList<>();
        for (Permission permission : permissionList) {
            if (!existsPermissionIdList.contains(permission.getId())) {
                tempPermissionList.add(permission);
            }
        }
        HashSet<Permission> set = new HashSet<>(tempPermissionList);
        return new ArrayList<>(set);
    }

    public static List<Permission> extractIncludeRemove(List<Long> permissionIncludeIdList, List<Permission> permissionList) {
        if (permissionIncludeIdList == null || permissionIncludeIdList.size() == 0) {
            return permissionList;
        }
        List<Permission> tempPermissionList = new ArrayList<>();
        for (Permission tempPermission : permissionList) {
            if (permissionIncludeIdList.contains(tempPermission.getId())) {
                tempPermissionList.add(tempPermission);
            }
        }
        HashSet<Permission> set = new HashSet<>(tempPermissionList);
        return new ArrayList<>(set);
    }
}